<?php
namespace app\admin\controller;

use think\Db;
use think\Session;
use think\Validate;

class Coupon extends Main
{
    /**
     * 管理员列表
    */
    public function index(){
        $where = [];
        $keyword = $this->request->param('keyword');
        if($keyword){
            $where[] = ['coupon_name|promotion_num|point','like','%'.$keyword.'%'];
        }
        $data = Db::name('coupon')
            ->where('action_id','=',input("id"))
            ->where($where)
            ->order('rank desc')
            ->paginate(15, false, [
                'query' => request()->param()]);
        $this->assign('list', $data);
        $this->assign('action_id', input("id"));
        return $this->fetch();
    }

    //增加广告
    public function addCoupon()
    {
        $request = $this->request;
        if($request->isPost()){
            $post     = $request->post();

//          $validate = validate('Action');
//          $res      = $validate->scene('add')->check($post);
//          if ($res !== true) {
//              $this->error($validate->getError());
//          } else {
                $post['createtime'] = date('Y-m-d H:i:s');
                $post['real_stock'] = $post['total_stock'];
                $db = Db::name('coupon')
                    ->insert($post);

                $this->success('添加成功');
//          }
        }else{
        	$this->assign('action_id', input("action_id"));
            return $this->fetch('add');
        }
    }

    //编辑提交
    public function editCoupon()
    {
        $request = $this->request;
        if($request->isPost()){
            $post     = $this->request->post();

//          $validate = validate('Admin');
//          $res = $validate->scene('edit')->check($post);
//          if ($res !== true) {
//              $this->error($validate->getError());
//          }
            $db = Db::name('coupon')
                ->where('id', $post['id'])
                ->update($post);

            $this->success('编辑成功');
        }else{
            $data = Db::name('coupon')
                ->where('id', input('id'))
                ->find();
            $this->assign('data', $data);
            return $this->fetch('edit');
        }

    }

    //删除用户
    /*public function deleteAdmin()
    {
        $id = $this->request->post('id');
        $username =  Db::name('activity')
            ->where('id',$id)
            ->value('username');
        if ((int) $id !== 1) {
            if($username!==session('username')){
                 $db = Db::name('admin')
                ->where('id', $id)
                ->setField('status');
                $this->success('删除成功');
            }else{
                 $this->error('无法删除当前登录用户');
            }
        } else {
            $this->error('超级管理员无法删除');
        }
    }*/

}
